from rest_framework import serializers

from apps.models import Comment


class CommentSerializer(serializers.ModelSerializer):
    comment_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', required=False)
    # 额外字段
    title = serializers.ReadOnlyField(source='thing.title')
    username = serializers.ReadOnlyField(source='user.username')

    class Meta:
        model = Comment
        fields = ['id', 'content', 'comment_time', 'like_count', 'thing', 'user', 'title', 'username']

    def create(self, validate_data):
        return Comment.objects.create(**validate_data)


class CommentDeleteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        fields = ['content', 'comment_time', 'like_count', 'thing', 'user']

    def create(self, validated_data):
        return Comment.objects.create(**validated_data)
